package linker.file;

import java.io.File;

import javax.swing.JFileChooser;

import linker.account.Account;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

/**
 * The fileFunction class used to send file and send plugin to the current buddy
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class FileFunction { 

	/**
	 * The sendFile function used to send file Static
	 * 
	 * @param JID
	 *            The full JID used to infor the current buddy
	 * @param file
	 *            The file to be sended
	 */
	public static void sendFile(String JID, File file) {
		FileTransferManager fileTransferManager = Account
				.getFileTransferManager();
		OutgoingFileTransfer sendFileRequest;
		if (JID != null) 
			sendFileRequest = fileTransferManager
					.createOutgoingFileTransfer(JID);
		else
			return;

		if (file == null) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(null);
			File temp = fileChooser.getSelectedFile();
			file = temp;
		}

		try {
			if (file != null) {
				sendFileRequest.sendFile(file, file.getName());
				FileManager.addSendFile(sendFileRequest);
			}
		} catch (XMPPException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * The send plugin function
	 * 
	 * @param JID
	 *            The JID which used to infor the current buddy
	 * @param file
	 *            The file which to be sended
	 */
	public static void sendPlugin(String JID, File file) {
		FileTransferManager fileTransferManager = Account
				.getFileTransferManager();
		OutgoingFileTransfer sendFileRequest;
		if (JID != null)  
			sendFileRequest = fileTransferManager
					.createOutgoingFileTransfer(JID);
		else 
			return;

		if (file == null) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(null);
			File temp = fileChooser.getSelectedFile();
			file = temp;
		}

		try {
			if (file != null) {
				sendFileRequest.sendFile(file, "Plugin" + file.getName());
				FileManager.addSendFile(sendFileRequest);
			}
		} catch (XMPPException e1) {
			e1.printStackTrace();
		}
	}

}
